package xyz.haijin.zt.core.db;


import xyz.haijin.zt.core.util.DbUtil;

import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;


/**
 * @author haijin
 */
public class DbFactory {

	private static String[][] configs;


	/**
	 * 初始化 获取配置文件中的数据库连接信息
	 */
	private void init() {
		if(configs == null) {
				Set<String> set = DbUtil.getNameSet();
				if(set.size() != 0) {
					configs = new String[set.size()][5];
					int i = 0;
					for(String key : set) {
						Map<String,String> contexts = DbUtil.getContext(key);
						String name = contexts.get("name");
						String url = contexts.get("url");
						String username = contexts.get("username");
						String password = contexts.get("password");
						String driver = contexts.get("driver");
						configs[i][0] = name;
						configs[i][1] = url;
						configs[i][2] = username;
						configs[i][3] = password;
						configs[i][4] = driver;
						i++;
					}
				}
		}
	}
	private static DbFactory instance;

	private HashMap<String, DbConnection> dbConnections = new HashMap<String, DbConnection>();

	/**
	 * 私有构造方法
	 */
	private DbFactory() {
		init();
		for (String[] config : configs) {
			DbConnection connection = new DbConnection(config[1], config[2], config[3], config[4]);
			dbConnections.put(config[0], connection);
		}
	}

	/**
	 * 单例获取数据库连接
	 * @param key
	 * @return
	 */
    public static DbConnection getInstance(String key) {
    	if(instance == null) {
    		instance = new DbFactory();
    	}
    	return instance.dbConnections.get(key);
    }

	/**
	 * 关闭数据库 声明
	 * @param statement
	 */
	public static void closeStatement(Statement statement) {
    	try {
			statement.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
    }

}
